﻿using Newtonsoft.Json;
using System.Net;
using System.Text;

namespace yongGu
{
    public class HttpHelper
    {
        public T Post<T,U>(string url,U obj) 
        {
            HttpWebRequest httpWebRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/json";
            Stream newStream = httpWebRequest.GetRequestStream();
            if (obj == null) return default(T);
            var json = JsonConvert.SerializeObject(obj);
            Config.JsonLogger.Debug("发去WMS的JSON：" + json);
            var byte1 = Encoding.UTF8.GetBytes(json);
            newStream.Write(byte1, 0, byte1.Length);
            HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse;
            var stream = response.GetResponseStream();
            T result = default(T);
            string str = "";
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                str = reader.ReadToEnd();
                result = JsonConvert.DeserializeObject<T>(str);
            }
            Config.JsonLogger.Debug("WMS返回的信息：" + str);
            return result;
        }
    }
}
